//
//  EpointBizTools.m
//  EpointFrame5
//
//  Created by 李亚春 on 14-1-24.
//  Copyright (c) 2014年 Epoint. All rights reserved.
//

#import "EpointBizTools.h"
#import "EpointAttachOpenViewController.h"
#import "TKAlertCenter.h"
#import "EncryptUtil.h"
#import "DBFrameService.h"

@implementation EpointBizTools

+(NSString *)getMobileOAWebserviceURL
{
    NSString *MobileManagerEnable = NSLocalizedString(@"MobileManagerEnable", @"");
    if ([MobileManagerEnable isEqualToString:@"1"]) {
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        return [userDefaults objectForKey:Epoint_MobileOAWebService_URL];
    }
    else
    {
        return NSLocalizedString(@"Epoint_DNet_WS", @"");
    }
    
}

+(NSString *)getRealDownloadUrl:(NSString *)durl
{
    NSString *oawsurl = [self getMobileOAWebserviceURL];
    NSString *realurl = [oawsurl stringByReplacingOccurrencesOfString:@"OAWebService.asmx" withString:durl];
    return realurl;
}

+(void)openAttachNavController:(UIViewController *)vc url:(NSString *)DownLoadUrl fileName:(NSString *)fileName folderGuid:(NSString *)folderGuid
{
    //0提示无法打开，1本地webview打开，2第三方app打开
    int aflag = 0;
    
    //支持webview打开的文件类型后缀
    NSArray *sfixs = [NSArray arrayWithObjects:@".doc",@".docx",@".xls",@".xlsx",@"ppt",@".pptx",@".pdf",@".pot",@".png",@".jpg",@"gif",@".txt",@".sql",@".bmp",@".xml",@".html", nil];
    
    for (NSString *s in sfixs) {
        if ([EpointStringUtil endWith:s forString:[fileName lowercaseString]]) {
            aflag = 1;
            break;
        }
    }
    
    if (aflag == 1) {
        NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
        [params setObject:DownLoadUrl forKey:@"fileurl"];
        [params setObject:fileName forKey:@"title"];
        [params setObject:folderGuid forKey:@"folderguid"];
        EpointAttachOpenViewController *ao = [[EpointAttachOpenViewController alloc]init];
        ao.passParam = params;
        [vc.navigationController pushViewController:ao animated:YES];
    }
    else
    {
        [[TKAlertCenter defaultCenter]postAlertWithMessage:@"该文件在移动设备无法打开！"];
    }
}

+(NSString *)getUserHeadImageUrl:(NSString *)userguid
{
    NSString *Epoint_MobileManager_URL = NSLocalizedString(@"Epoint_MobileManager_URL", @"");
    Epoint_MobileManager_URL = [Epoint_MobileManager_URL stringByReplacingOccurrencesOfString:@"services/MobileOaManage?wsdl" withString:[NSString stringWithFormat:@"interface.jspx?mbtype=com.epoint.mobile.httpbiz.MobileCommonService;getUserPhoto&userguid=%@",userguid]];
    return Epoint_MobileManager_URL;
}

+(NSString *)getUrl:(NSString *)pattern
{
    NSString *Epoint_MobileManager_URL = NSLocalizedString(@"Epoint_MobileManager_URL", @"");
    Epoint_MobileManager_URL = [Epoint_MobileManager_URL stringByReplacingOccurrencesOfString:@"services/MobileOaManage?wsdl" withString:pattern];
    return Epoint_MobileManager_URL;
}

+(NSString *)getUrlTest:(NSString *)pattern
{
    NSString *Epoint_MobileManager_URL = @"http://192.168.1.187/tczjj_ws/mobileconfig/ios.xml";
    Epoint_MobileManager_URL = [Epoint_MobileManager_URL stringByReplacingOccurrencesOfString:@"mobileconfig/ios.xml" withString:pattern];
    return Epoint_MobileManager_URL;
}

+(NSString *)getUserHeadImageDataUrl:(NSString *)userguid
{
    NSString *headUrl = [EpointBizTools getUrl:[NSString stringWithFormat:@"mobiletmp/%@.jpg",userguid]];
    return headUrl;
}

+(NSString *)getToken
{
    NSString *token = [[[EncryptUtil getToken:EpointWS_AppKey secret:EpointWS_AppSecret] stringByReplacingOccurrencesOfString:@"+" withString:@"-"]stringByReplacingOccurrencesOfString:@"/" withString:@"_"];
    return token;
}

+(NSString *)getEpointZeroReportWebserviceURL
{
//    return @"http://221.224.118.93:8060/ZreportService/ZreportServer.asmx";
//    return @"http://oa.epoint.com.cn/ZReportService/ZreportServer.asmx";
//    return @"http://192.168.188.82/ZreportService/ZreportServer.asmx";
    return @"http://oa.epoint.com.cn/ZreportServiceV2/ZreportServer.asmx";
}

+(NSString *)getZReportDownloadUrl:(NSString *)durl
{
    NSString *oawsurl = [self getEpointZeroReportWebserviceURL];
    NSString *realurl = [oawsurl stringByReplacingOccurrencesOfString:@"ZreportServer.asmx" withString:durl];
    return realurl;
}

+(NSString *)getEpointZeroReportWebserviceNameSpace
{
    return @"http://tempuri.org/";
}

+(NSString *)getEpointZeroReportUserGuid
{
//    return @"45f0c5f9-cad2-49e6-887d-b38dfcbc23de";
//    return @"e35d2ce9-166e-41bc-bc30-54df24b624ca";
    return [DBFrameService getUserGuid];
}

+(NSString *)saveAttachLib:(NSData *)fileData folderGuid:(NSString *)folderGuid fileName:(NSString *)fileName
{
    NSString *mainPath = [NSString stringWithFormat:@"%@/Documents/attaches",NSHomeDirectory()];
    NSString* folderPath = [NSString stringWithFormat:@"%@/%@",mainPath,folderGuid];
    NSString *fullPath = [NSString stringWithFormat:@"%@/%@",folderPath,fileName];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    [fileManager createDirectoryAtPath:folderPath withIntermediateDirectories:YES attributes:nil error:nil];
    [fileData writeToFile:fullPath atomically:NO];
    return fullPath;
}

@end
